<?php
class Muser extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	function loadUser($realname,$provinceid,$cityid,$sex,$minAge,$maxAge,$idstatus,$status){
		$sql = "select ui.*,p.name provincename,c.name cityname,p1.name jgprovincename,c1.name jgcityname,
				(YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5))   AS age,
				d.name idcardtypename,d1.name idstatusname,case when ui.status = 0 then '否' else '是' end statusname,
				ifnull(phone,email) username
				from tbuserinfo ui
				left join tbprovince p on ui.provinceid= p.id
				left join tbcity c on ui.cityid = c.id
				left join tbprovince p1 on ui.jgprovinceid = p1.id
				left join tbcity c1 on ui.jgcityid = c1.id
				left join tbdic d on ui.idcardtype=d.classid and d.type=7
				left join tbdic d1 on ui.idcardstatus = d1.classid and d1.type=8
				where 1 = 1";
		$arr = array();
		if($realname!=""){
			$sql.=" and ui.realname = ?";
			$arr[] = $realname;
		}
		if($provinceid!=""){
			$sql.=" and ui.provinceid = ?";
			$arr[] = $provinceid;
		}
		if($cityid!=""){
			$sql.=" and ui.cityid = ?";
			$arr[] = $cityid;
		}
		if($sex!=""){
			$sql.=" and ui.sex = ?";
			$arr[] = $sex;
		}
		if($minAge!=""){
			$sql.=" and date_add(curdate(),interval -? year) >= ui.birthday";
			$arr[] = $minAge;
		}
		if($maxAge!=""){
			$sql.=" and date_add(curdate(),interval -? year) <= ui.birthday";
			$arr[] = $maxAge;
		}
		if($idstatus!=""){
			$sql.=" and ui.idcardstatus = ?";
			$arr[] = $idstatus;
		}
		if($status!=""){
			$sql.=" and ui.status = ?";
			$arr[] = $status;
		}
		$query = $this->db->query($sql,$arr);

		return $query->result();
	}

	function delUser($ids){
		$sql = "delete from tbuserinfo where id in $ids";
		$this->db->query($sql);
		return $this->db->affected_rows();
	}

	function getById($id){
		$arr = array();
		$sql = "select ui.*,p.name provincename,c.name cityname,p1.name jgprovincename,c1.name jgcityname,
				(YEAR(CURDATE())-YEAR(birthday)) - (RIGHT(CURDATE(),5)<RIGHT(birthday,5))   AS age,
				ifnull(phone,email) username,w.url,d1.name idcardtypename,d2.name idcardstatusname
				from tbuserinfo ui
				left join tbprovince p on ui.provinceid= p.id
				left join tbcity c on ui.cityid = c.id
				left join tbprovince p1 on ui.jgprovinceid = p1.id
				left join tbcity c1 on ui.jgcityid = c1.id
				left join tbweibo w on ui.id=w.uid and wid=1
				left join tbdic d1 on ui.idcardtype=d1.classid and d1.type=7
				left join tbdic d2 on ui.idcardstatus=d2.classid and d2.type=8
				where ui.id = ?";
		$query = $this->db->query($sql,array("$id"));
		foreach ($query->result() as $row){
			$arr["id"]=empty($row->id)?"":$row->id;
			$arr["username"] = $row->username;
			$arr["realname"]=empty($row->realname)?"":$row->realname;
			$arr["birthday"]=empty($row->birthday)?"":$row->birthday;
			$arr["email"]=empty($row->email)?"":$row->email;
			$arr["phone"]=empty($row->phone)?"":$row->phone;
			$arr["qq"]=empty($row->qq)?"":$row->qq;
			$arr["pwd"]=empty($row->pwd)?"":$row->pwd;
			$arr["checkcode"]=empty($row->checkcode)?"":$row->checkcode;
			$arr["sex"]=empty($row->sex)?"":$row->sex;
			$arr["status"]=empty($row->status)?"":$row->status;
			$arr["headurl"]=empty($row->headurl)?"":$row->headurl;
			$arr["cardid"]=empty($row->cardid)?"":$row->cardid;
			$arr["provinceid"]=empty($row->provinceid)?"":$row->provinceid;
			$arr["provincename"] = empty($row->provincename)?"":$row->provincename;
			$arr["cityid"]=empty($row->cityid)?"":$row->cityid;
			$arr["cityname"] = empty($row->cityname)?"":$row->cityname;
			$arr["jgprovinceid"]=empty($row->jgprovinceid)?"":$row->jgprovinceid;
			$arr["jgprovincename"] = empty($row->jgprovincename)?"":$row->jgprovincename;
			$arr["jgcityid"]=empty($row->jgcityid)?"":$row->jgcityid;
			$arr["jgcityname"] = empty($row->jgcityname)?"":$row->jgcityname;
			$arr["high"]=empty($row->high)?"":$row->high;
			$arr["weight"]=empty($row->weight)?"":$row->weight;
			$arr["shoesize"]=empty($row->shoesize)?"":$row->shoesize;
			$arr["chestsize"]=empty($row->chestsize)?"":$row->chestsize;
			$arr["waistsize"]=empty($row->waistsize)?"":$row->waistsize;
			$arr["buttsize"]=empty($row->buttsize)?"":$row->buttsize;
			$arr["worknature"]=empty($row->worknature)?"":$row->worknature;
			$arr["modeltype"]=empty($row->modeltype)?"":$row->modeltype;
			$arr["modelstyle"]=empty($row->modelstyle)?"":$row->modelstyle;
			$arr["modelhair"]=empty($row->modelhair)?"":$row->modelhair;
			$arr["modelskin"]=empty($row->modelskin)?"":$row->modelskin;
			$arr["modelbody"]=empty($row->modelbody)?"":$row->modelbody;
			$arr["activitiexperience"]=empty($row->activitiexperience)?"":$row->activitiexperience;
			$arr["trainingexperience"]=empty($row->trainingexperience)?"":$row->trainingexperience;
			$arr["competitionexperience"]=empty($row->competitionexperience)?"":$row->competitionexperience;
			$arr["reward"]=empty($row->reward)?"":$row->reward;
			$arr["cardcontent"] = empty($row->cardcontent)?"":$row->cardcontent;
			$arr["age"] = !isset($row->age)?"":$row->age;
			$arr["idcardurl"] = !isset($row->idcardurl)?"":$row->idcardurl;
			$arr["idcardurl2"] = !isset($row->idcardurl2)?"":$row->idcardurl2;
			$arr["idcardtype"] = !isset($row->idcardtype)?"":$row->idcardtype;
			$arr["idcardtypename"] = !isset($row->idcardtypename)?"":$row->idcardtypename;
			$arr["idcardno"] = !isset($row->idcardno)?"":$row->idcardno;
			$arr["idcardremark"] = !isset($row->idcardremark)?"":$row->idcardremark;
			$arr["idcardtime"] = !isset($row->idcardtime)?"":$row->idcardtime;

			$arr["idcardstatus"] = !isset($row->idcardstatus)?"":$row->idcardstatus;
			$arr["idcardstatusname"] = !isset($row->idcardstatusname)?"":$row->idcardstatusname;
			$arr["isvalid"] = $row->isvalid;
			$arr["weibo"] = $row->url;
			//增加模特类型
			$arr["modeltypename"]="";
			$result = "";
			$modeltype = $arr["modeltype"];
			$arrType = explode("/", $modeltype);
			$type= 2;
			foreach ($arrType as $classid){
				if($classid!=""){
					$sqlType = "select *
							from tbdic where classid = ? and type = 2";
					$queryType = $this->db->query($sqlType,array("$classid"));
					foreach ($queryType->result() as $rowType){
						$arr["modeltypename"].="  ".$rowType->name;
					}
				}
			}
			return $arr;
		}
		return $arr;
	}
	function modifyUser( $id, $realname, $birthday, $provinceid, $cityid, $jgprovinceid, $jgcityid, $phone,$email, $pwd,$qq, $sex,$isvalid ,$iconId){
		$sql = "update tbuserinfo
				set realname= ?,birthday = ?,provinceid = ?,cityid = ?,jgprovinceid = ?,jgcityid = ?,
					phone = ?,email = ?,pwd = ?,qq = ?, sex = ?,isvalid = ?
				where id = ?";
		$query = $this->db->query($sql,array("$realname","$birthday","$provinceid","$cityid",
				"$jgprovinceid","$jgcityid","$phone","$email","$pwd","$qq","$sex","$isvalid","$id"));
		if(!empty($iconId)){
            $this->Musericon->addUserIcon($id,$iconId);
        }
        return $this->db->affected_rows();
	}

	function modifyIdCard($id,$idcardremark,$idcardstatus){
		$sql = "update tbuserinfo
				set idcardremark = ?,idcardstatus = ?,idcardtime = now()
				where id = ?";
		$this->db->query($sql,array("$idcardremark","$idcardstatus","$id"));
		return $this->db->affected_rows();
	}
	function saveArchiceOne ( $id, $realname, $birthday, $provinceid, $cityid,
			$jgprovinceid, $jgcityid, $phone, $qq,$sex ){
		$sql = "update tbuserinfo
				set realname= ?,birthday = ?,provinceid = ?,cityid = ?,jgprovinceid = ?,jgcityid = ?,
					phone = ?,qq = ?, sex = ?
				where id = ?";
		$query = $this->db->query($sql,array("$realname","$birthday","$provinceid","$cityid",
				"$jgprovinceid","$jgcityid","$phone","$qq","$sex","$id"));
		return $this->db->affected_rows();
	}

	function saveArchiveTwo($id,$high,$weight,$shoesize,$chestsize,$waistsize,$buttsize,
			$worknature,$modeltype,$modelstyle,$modelhair,$modelskin,$modelbody){
		$sql = "update tbuserinfo
				set high = ?,weight = ?,shoesize = ?,chestsize = ?,waistsize = ?, buttsize = ?,
					worknature = ?,modeltype = ?,modelstyle = ?,modelhair = ?,modelskin = ?,modelbody = ?
				where id = ?";
		$query = $this->db->query($sql,array("$high","$weight","$shoesize","$chestsize","$waistsize","$buttsize",
				"$worknature","$modeltype","$modelstyle","$modelhair","$modelskin","$modelbody","$id"));
		return $this->db->affected_rows();
	}
	function saveExperience($id,$activity,$training,$competition,$reward){
		$sql = "update tbuserinfo
				set activitiexperience = ?,trainingexperience = ?,competitionexperience = ?,reward = ?
				where id = ?";
		$query = $this->db->query($sql,array("$activity","$training","$competition","$reward","$id"));
		return $this->db->affected_rows();
	}
	function saveCard($id,$cardid,$cardcontent){
		$sql = "update tbuserinfo
				set cardid = ?,cardcontent = ?
				where id = ?";
		$query = $this->db->query($sql,array("$cardid","$cardcontent","$id"));
		return $this->db->affected_rows();
	}
	function checkPwd($id,$pwd){
		$sql = "select *
				from tbuserinfo
				where id = ? and pwd = ?";
		$query = $this->db->query($sql,array("$id","$pwd"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function modifyPwd($id,$pwd,$pwdnew){
		$sql = "update tbuserinfo
				set pwd = ?
				where id= ? and pwd = ?";
		$this->db->query($sql,array("$pwdnew","$id","$pwd"));
		return $this->db->affected_rows();
	}
	function updateIdCard($id,$url){
		$sql = "update tbuserinfo
				set idcardurl = ?,idcardstatus = 0
				where id = ?";
		$this->db->query($sql,array("$url","$id"));
		return $this->db->affected_rows();
	}
	function saveModifyPhone ($id, $phone, $code ){
		$sql = "update tbuserinfo
				set modifyphone = ?, checkcode = ?
				where id = ?";
		$this->db->query($sql,array("$phone","$code","$id"));
		return $this->db->affected_rows();
	}
	function checkModifyCode($phone ,$code){
		$sql = "select *
				from tbuserinfo
				where modifyphone = ? and checkcode = ?";
		$query = $this->db->query($sql,array("$phone","$code"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	function updateModifyPhone($id){
		$sql = "update tbuserinfo
				set phone = modifyphone
				where id = ?";
		$this->db->query($sql,array("$id"));
		return $this->db->affected_rows();
	}
	function modifyUserGroup($ids,$gid){
		$sql = "update tbuserinfo
				set groupid = $gid
				where id in $ids";
		
		//$this->db->query($sql);
		$this->db->query($sql,array("$gid"));
	}
	
}
?>